#include "muduo/proto/codec.h"
#include "muduo/proto/dispatcher.h"
#include "requst.pb.h"
#include "muduo/base/Logging.h"
#include "muduo/net/EventLoop.h"
#include "muduo/net/EventLoopThreadPool.h"
#include "muduo/net/TcpServer.h"

#include <memory>
#include <iostream>
#include <functional>
class Server
{
public:
    typedef std::shared_ptr<google::protobuf::Message> MessagePtr;
    typedef std::shared_ptr<cqy::TranslateRequest> TreanslateRequestPtr;
    typedef std::shared_ptr<cqy::AddRequest> AddRequestPtr;
    
    Server(int16_t port)
        : _baseserver(&_eventloop, muduo::net::InetAddress("0.0.0.0", port), "Server", muduo::net::TcpServer::kNoReusePort),
          _dispatcher(std::bind(&Server::onUnknownMessage, this, std::placeholders::_1,
                                std::placeholders::_2, std::placeholders::_3)),
          _codec(std::bind(&ProtobufDispatcher::onProtobufMessage, &_dispatcher, 
                std::placeholders::_1, std::placeholders::_2, std::placeholders::_3))
    {
        //注册消息处理回调
        _dispatcher.registerMessageCallback<cqy::TranslateRequest>(std::bind(&Server::onTranslate, this, 
                std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
        _dispatcher.registerMessageCallback<cqy::AddRequest>(std::bind(&Server::onAdd, this, 
                std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
        //设置连接处理回调
        _baseserver.setConnectionCallback(std::bind(&Server::onConnection,this,std::placeholders::_1));
        //设置消息回调链
        _baseserver.setMessageCallback(std::bind(&ProtobufCodec::onMessage,&_codec,
            std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
    }
    ~Server() {}
    void Start()
    {
        _baseserver.start();
        _eventloop.loop();
    }
private:
    void onUnknownMessage(const muduo::net::TcpConnectionPtr &conn, const MessagePtr &message, muduo::Timestamp)
    {
        LOG_INFO << "onUnknownMessage: " << message->GetTypeName();
        conn->shutdown();
    }
    void onConnection(const muduo::net::TcpConnectionPtr &conn)
    {
        if(conn->connected())
        {
            LOG_INFO <<"connect success";
        }
        else
        {
            LOG_INFO<<"connect close";
        }
    }
    std::string translate(const std::string &str) {
            static std::unordered_map<std::string, std::string> dict_map = {
                {"hello", "你好"},
                {"Hello", "你好"},
                {"你好", "Hello"},
                {"吃了吗", "油泼面"}
            };
            auto it = dict_map.find(str);
            if (it == dict_map.end()) {
                return "没听懂！！";
            }
            return it->second;
        }
    void onTranslate(const muduo::net::TcpConnectionPtr &conn, const TreanslateRequestPtr &message, muduo::Timestamp)
    {
        //提取请求
        std::string req_msg = message->msg();
        //对请求进行处理
        std::string resp_msg = translate(req_msg);
        //将结果序列化
        cqy::TranslateResponse response;
        response.set_msg(resp_msg);
        //发送
        _codec.send(conn,response);
    }

    void onAdd(const muduo::net::TcpConnectionPtr &conn, const AddRequestPtr &message, muduo::Timestamp)
    {
        int num1 = message->num1();
        int num2 = message->num2();
        int result = num1+num2;
        cqy::AddResponse resp;
        resp.set_result(result);
        _codec.send(conn,resp);
    }
private:
    muduo::net::EventLoop _eventloop;//进行读数据监控
    muduo::net::TcpServer _baseserver;//服务器
    ProtobufDispatcher _dispatcher; // 请求分发器对象--向其注册请求处理函数
    ProtobufCodec _codec;           // 协议处理器，针对不同的请求使用不同的处理方法
};
int main()
{
    Server server(8080);
    server.Start();
    return 0;
}